package com.broadengate.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.broadengate.form.TransferForm;
import com.broadengate.model.Card;
import com.broadengate.model.Atminfo;
import com.broadengate.model.Transfer;
import com.broadengate.service.AdminService;
import com.broadengate.service.CardService;
import com.broadengate.service.CurrencyService;
import com.broadengate.service.TransferService;

public class TransferAction extends DispatchAction {
	private TransferService transferService;
	private CardService cardService;
	private CurrencyService currencyService;
	private AdminService adminService;
	
	/******
	 * 实现用户转账功能
	 * 核心功能由调用transferService对象的方法trans();cardService对象的方法findCardByCardNO()；currencyService对象的方法findCurrencyByName实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward trans(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		    HttpSession session=request.getSession();
		    System.out.print("进入转账");
		    TransferForm transferForm = (TransferForm) form;
		    Atminfo atminfo=new Atminfo();
			float ammout = Float.parseFloat(transferForm.getTranmoney());
			int i=(int)ammout;
			//System.out.print(adminService.findAtminfo().getPoundage());
			//float info=adminService.findAtminfo().getPoundage();
			float poundage=ammout*adminService.findAtminfo().getPoundage();
					
			Card cardS=cardService.findCardByCardNO(request.getSession().getAttribute("cardNO").toString());
			Card cardD=cardService.findCardByCardNO(transferForm.getCardno());
			
			if(cardService.findCardByCardNO(transferForm.getCardno())==null){
				session.setAttribute("wrong","CNH");
				return mapping.findForward("fail");
			}
			
			if((cardS.getBank().getId()).equals(cardD.getBank().getId())==false)
				poundage=poundage+2;
			String currencyName="人民币";
			if(	transferForm.getSave().equals("rmb")){
				currencyName="人民币";
				if((ammout) <= (cardS.getRmbbalance()))
				{
				  cardS.setRmbbalance(cardS.getRmbbalance()-ammout-poundage);
				  cardD.setRmbbalance(cardD.getRmbbalance()+ammout);
				}
				else
				{
					session.setAttribute("wrong","BNE");
					return mapping.findForward("fail");
				}
			}
			else if(transferForm.getSave().equals("dollar")){
				currencyName="美元";
				if((ammout) <= (cardS.getDollarBalance()))
				{
				  cardS.setDollarBalance(cardS.getDollarBalance()-ammout-poundage);
				  cardD.setDollarBalance(cardD.getDollarBalance()+ammout);
				}
				else
				{
					session.setAttribute("wrong","BNE");
					return mapping.findForward("fail");
				}
			}
			else if(transferForm.getSave().equals("euro")){
				currencyName="欧元";
				if((ammout) <= (cardS.getEuroBalance()))
				{
				  cardS.setEuroBalance(cardS.getEuroBalance()-ammout-poundage);
				  cardD.setEuroBalance(cardD.getEuroBalance()+ammout);
				}
				else
				{
					session.setAttribute("wrong","BNE");
					return mapping.findForward("fail");
				}
			}

			Transfer transfer=new Transfer();
			transfer.setPoundage(poundage);
			transfer.setAmount(ammout);
			transfer.setCardByDestinedCardId(cardD);
			transfer.setCardBySourceCardId(cardS);
			transfer.setCurrency(currencyService.findCurrencyByName(currencyName));
			
			transfer.setTime(new Date());
			
			try {
				transferService.trans(cardD, cardS, transfer);
				return mapping.findForward("success");
			} catch (Exception e) {
				e.printStackTrace();
				return mapping.findForward("fail");
			}
	}

	public TransferService getTransferService() {
		return transferService;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
}
